package redora.rdo.client.data;


import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class DataVLayout extends VLayout {

	DynamicForm langForm = new DynamicForm();

	ListGrid dataList = new ListGrid();

	DynamicForm form = new DynamicForm();

	String objectName = "";

	String methodName = "";

	String methodField = "";

	DataSource dataSource;

	public DataVLayout(String objectName, final String methodName,
			final String methodField, DataSource source) {
		this.objectName = objectName;
		this.methodName = methodName;
		this.methodField = methodField;
		this.dataSource = source;
	}

	public void init() {
		this.setWidth100();
		this.setHeight100();

		// form for add model
		form.setWidth(500);
		form.setUseAllDataSourceFields(true);
		form.setNumCols(4);
		form.setDataSource(dataSource);

		// gridlist for model data
		dataList.setSelectionAppearance(SelectionAppearance.CHECKBOX);
		dataList.setSelectionType(SelectionStyle.SIMPLE);

		dataList.setDataSource(dataSource);
		dataList.setWidth100();
		dataList.setHeight(600);
		dataList.setAutoFetchData(true);
		dataList.setAlternateRecordStyles(true);
		dataList.addRecordDoubleClickHandler(new RecordDoubleClickHandler() {

			public void onRecordDoubleClick(RecordDoubleClickEvent event) {
				dataList.deselectAllRecords();
				dataList.selectRecord(event.getRecord());
				form.editSelectedData(dataList);
				final Window winModal = new Window();
				winModal.setWidth(550);
				winModal.setHeight(300);
				winModal.setTitle("Update");
				winModal.setShowMinimizeButton(false);
				winModal.setIsModal(true);
				winModal.setShowModalMask(true);
				winModal.centerInPage();
				winModal.addCloseClickHandler(new CloseClickHandler() {
					public void onCloseClick(CloseClientEvent event) {
						winModal.destroy();
					}
				});
				winModal.addItem(form);
				HLayout layout = new HLayout();
				IButton saveButton = new IButton("Save");
				IButton cancelButton = new IButton("Cancel");
				layout.addMember(saveButton);
				layout.addMember(cancelButton);
				saveButton.setLeft(200);
				saveButton.setTop(600);
				saveButton.setWidth(150);
				saveButton.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						if (!form.validate()) {
							return;
						}
						form.saveData();
						if (!form.hasErrors()) {
							form.clearValues();
						}
						dataList.getResultSet().invalidateCache();
						dataList.fetchData();
						winModal.destroy();
					}
				});
				cancelButton.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						winModal.destroy();
					}
				});
				winModal.addItem(layout);
				winModal.show();
			}

		});

		HLayout topHLayout = new HLayout();
		topHLayout.setHeight(25);
		HLayout bottomHLayout = new HLayout();
		topHLayout.addMember(langForm);
		if (methodField != null && methodField.length() > 0) {
			final TextItem search = new TextItem();
			search.setName(methodField);
			search.setTitle(methodField);
			IButton searchButton = new IButton("search");
			searchButton.addClickHandler(new ClickHandler() {

				public void onClick(ClickEvent event) {
					Criteria criteria = new Criteria();
					criteria.addCriteria(methodField, search.getValue()
							.toString());
					dataList.getResultSet().invalidateCache();
					dataList.fetchData(criteria);
				}

			});
			langForm.setItems(search);
			topHLayout.addMember(searchButton);
		}
		langForm.setNumCols(6);
		langForm.setLeft(600);

		// three buttons for model data insert delete
		IButton removeSelected = new IButton("Delete");
		removeSelected.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				final ListGridRecord[] selectedRecords = dataList
						.getSelection();
				if (selectedRecords != null && selectedRecords.length > 0) {
					SC.confirm("You will delete " + selectedRecords.length
							+ " records", new BooleanCallback() {
						public void execute(Boolean value) {
							if (value) {
								for (ListGridRecord rec : selectedRecords) {
									dataList.removeData(rec);
								}
								dataList.getResultSet().invalidateCache();
								dataList.fetchData();
								form.reset();
							}
						}
					});
				} else {
					SC.warn("Select a record before performing this action");
				}
			}

		});
		IButton addNewButton = new IButton("Add New");
		addNewButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				final Window winModal = new Window();
				winModal.setWidth(550);
				winModal.setHeight(300);
				winModal.setTitle("Add New");
				winModal.setShowMinimizeButton(false);
				winModal.setIsModal(true);
				winModal.setShowModalMask(true);
				winModal.centerInPage();
				winModal.addCloseClickHandler(new CloseClickHandler() {
					public void onCloseClick(CloseClientEvent event) {
						winModal.destroy();
					}
				});
				form.reset();
				winModal.addItem(form);
				HLayout layout = new HLayout();
				IButton saveButton = new IButton("Save");
				IButton cancelButton = new IButton("Cancel");
				layout.addMember(saveButton);
				layout.addMember(cancelButton);
				saveButton.setWidth(150);
				saveButton.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						if (!form.validate()) {
							return;
						}
						form.saveData();
						if (!form.hasErrors()) {
							form.clearValues();
						}
						dataList.getResultSet().invalidateCache();
						dataList.fetchData();
						winModal.destroy();
					}
				});
				cancelButton.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						winModal.destroy();
					}
				});
				winModal.addItem(layout);
				winModal.show();
			}
		});
		IButton updateButton = new IButton("Update");
		updateButton.addClickHandler(new UpdateHandler());
		IButton deSelectButton = new IButton("Deselect");
		deSelectButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				dataList.deselectAllRecords();
			}

		});

		bottomHLayout.addMember(addNewButton);
		bottomHLayout.addMember(updateButton);
		bottomHLayout.addMember(removeSelected);
		bottomHLayout.addMember(deSelectButton);

		this.addMember(topHLayout);
		this.addMember(dataList);
		this.addMember(bottomHLayout);
		// this.draw();
	}

	public class UpdateHandler implements ClickHandler {

		public void onClick(ClickEvent event) {
			if (dataList.getSelection().length != 1) {
				SC.warn("Please select one record for update!");
				return;
			}
			form.editSelectedData(dataList);
			final Window winModal = new Window();
			winModal.setWidth(550);
			winModal.setHeight(300);
			winModal.setTitle("Update");
			winModal.setShowMinimizeButton(false);
			winModal.setIsModal(true);
			winModal.setShowModalMask(true);
			winModal.centerInPage();
			winModal.addCloseClickHandler(new CloseClickHandler() {
				public void onCloseClick(CloseClientEvent event) {
					winModal.destroy();
				}
			});
			winModal.addItem(form);
			HLayout layout = new HLayout();
			IButton saveButton = new IButton("Save");
			IButton cancelButton = new IButton("Cancel");
			layout.addMember(saveButton);
			layout.addMember(cancelButton);
			saveButton.setLeft(200);
			saveButton.setTop(600);
			saveButton.setWidth(150);
			saveButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					if (!form.validate()) {
						return;
					}
					form.saveData();
					if (!form.hasErrors()) {
						form.clearValues();
					}
					dataList.getResultSet().invalidateCache();
					dataList.fetchData();
					winModal.destroy();
				}
			});
			cancelButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					winModal.destroy();
				}
			});
			winModal.addItem(layout);
			winModal.show();

		}

	}

	public void clear() {
		this.clear();
	}

}
